package com.kanqiubao.kq;

import androidx.fragment.app.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.dueeeke.videoplayer.player.VideoViewManager;
import com.kanqiubao.kq.activity.BaseActivity;
import com.kanqiubao.kq.activity.ramking.RankingMainActivity;
import com.kanqiubao.kq.activity.recoding.RecodingSelectDayActivity;
import com.kanqiubao.kq.activity.search.SearchMainActivity;
import com.kanqiubao.kq.activity.user.LoginMainActivity;
import com.kanqiubao.kq.activity.user.RegisterActivity;
import com.kanqiubao.kq.activity.user.SettingMainActivity;
import com.kanqiubao.kq.activity.user.UserCenterActvity;
import com.kanqiubao.kq.activity.user.UserDjActivity;
import com.kanqiubao.kq.activity.user.UserHistoryListActivity;
import com.kanqiubao.kq.activity.user.UserQpListActivity;
import com.kanqiubao.kq.activity.user.UserRwActivity;
import com.kanqiubao.kq.fragment.NewsFragmentStatePagerAdapter;
import com.kanqiubao.kq.fragment.main.MainFragmentFive;
import com.kanqiubao.kq.fragment.main.MainFragmentFour;
import com.kanqiubao.kq.fragment.main.MainFragmentOne;
import com.kanqiubao.kq.fragment.main.MainFragmentThree;
import com.kanqiubao.kq.fragment.main.MainFragmentTwo;
import com.kanqiubao.kq.utils.sharepreferences.SharedPreferencesUtil;
import com.kanqiubao.kq.view.NoScrollViewPager;
import com.kanqiubao.kq.view.buttombar.AlphaTabsIndicator;
import com.kanqiubao.kq.view.buttombar.OnTabChangedListner;
import com.lxj.xpopup.XPopup;

import java.util.ArrayList;

public class MainActivity extends BaseActivity<String> implements OnTabChangedListner {
    private AlphaTabsIndicator mAlphaTabsIndicator;
    private NoScrollViewPager mNoScrollViewPager;
    private NewsFragmentStatePagerAdapter mNewsFragmentStatePagerAdapter;
    private ArrayList<Fragment> mListFragment;

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        mAlphaTabsIndicator = findView(R.id.alphaIndicator);
        mNoScrollViewPager = findView(R.id.main_page);
        mNoScrollViewPager.setNoScroll(true);
        mAlphaTabsIndicator.setOnTabChangedListner(this);
        initMainFragment();
    }

    @Override
    protected int getActivitylayout() {
        return R.layout.activity_main;
    }

    @Override
    protected String getTitleText() {
        return null;
    }

    private void initMainFragment() {
        mListFragment = new ArrayList<>();
        MainFragmentOne fg1 = new MainFragmentOne();
        MainFragmentTwo fg2 = new MainFragmentTwo();
        MainFragmentThree fg3 = new MainFragmentThree();
        MainFragmentFour fg4 = new MainFragmentFour();
        MainFragmentFive fg5 = new MainFragmentFive();
        mListFragment.add(fg1);
        mListFragment.add(fg2);
        mListFragment.add(fg3);
        mListFragment.add(fg4);
        mListFragment.add(fg5);
        mNewsFragmentStatePagerAdapter = new NewsFragmentStatePagerAdapter(getSupportFragmentManager(), mListFragment);
        mNoScrollViewPager.setAdapter(mNewsFragmentStatePagerAdapter);
        mNoScrollViewPager.setOffscreenPageLimit(5);
        mAlphaTabsIndicator.setViewPager(mNoScrollViewPager);
    }

    @Override
    public void click(View v) {
        switch (v.getId()) {
            case R.id.zb_ph_id:
                startMyActivity(RankingMainActivity.class);
                break;
            case R.id.lx_ph_id:
                startMyActivity(RecodingSelectDayActivity.class);
                break;
            case R.id.search_click_root_id:
                startMyActivity(SearchMainActivity.class);
                break;
            case R.id.zb_search_txt_id:
                startMyActivity(SearchMainActivity.class);
                break;
            case R.id.lx_search_txt_id:
                Intent in = new Intent();
                in.putExtra("type", 1);
                startMyActivity(in, SearchMainActivity.class);
                break;
            case R.id.video_search_click_root_id:
                Intent in1 = new Intent();
                in1.putExtra("type", 2);
                startMyActivity(in1, SearchMainActivity.class);
                break;
            case R.id.login_name:
                //沒登陸直接而去登錄

                break;
            case R.id.face_root:
//                if(!SharedPreferencesUtil.getInstance().getIsLogin()){
//                    alert();
//                    return;
//                }
                startMyActivity(UserCenterActvity.class);
                break;
            case R.id.grzl:
//                if(!SharedPreferencesUtil.getInstance().getIsLogin()){
//                    alert();
//                    return;
//                }
                startMyActivity(UserCenterActvity.class);
                break;
            case R.id.dj:
//                if(!SharedPreferencesUtil.getInstance().getIsLogin()){
//                    alert();
//                    return;
//                }
                //经验值
                startMyActivity(UserDjActivity.class);
                break;
            case R.id.rw:
//                if(!SharedPreferencesUtil.getInstance().getIsLogin()){
//                    alert();
//                    return;
//                }
                startMyActivity(UserRwActivity.class);
                break;
            case R.id.his:
//                if(!SharedPreferencesUtil.getInstance().getIsLogin()){
//                    alert();
//                    return;
//                }
                startMyActivity(UserHistoryListActivity.class);
                break;
            case R.id.sz:
                startMyActivity(SettingMainActivity.class);
                break;
            case R.id.user_gz_id:
                mNoScrollViewPager.setCurrentItem(3);
                mAlphaTabsIndicator.test(3);
                break;
            case R.id.user_yy_id:
                //预约
                break;
            case R.id.user_jyz_id:
                //经验值
                startMyActivity(UserDjActivity.class);
                break;
            case R.id.user_qp_id:
                //经验值
                startMyActivity(UserQpListActivity.class);
                break;

        }
        super.click(v);
    }

    @Override
    public void onTabSelected(int tabNum) {
        if (tabNum != 3) {
            VideoViewManager.instance().releaseByTag("list");
        }
    }
    private void alert() {
        new XPopup.Builder(this).asConfirm("提示", "你还没有登录，是否登录？",
                () -> {
                    //确认
                    startMyActivity(LoginMainActivity.class);
                }, () -> {
                })
                .show();
    }
}